package com.caipos.actor.ruleChain;

import com.caipos.actor.ActorSystemContext;
import com.caipos.actor.common.actors.KbActorRef;
import com.caipos.actor.common.actors.KbEntityActorId;
import com.caipos.actor.common.data.EntityType;
import com.caipos.actor.common.data.id.EntityId;
import com.caipos.actor.common.data.id.RuleChainId;
import com.caipos.actor.common.data.id.TenantId;
import com.caipos.actor.common.data.rule.RuleChain;
import com.caipos.actor.service.ContextAwareActor;
import com.caipos.actor.service.DefaultActorService;
import com.caipos.service.RuleChainService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.function.Function;

@Slf4j
public abstract class RuleChainManagerActor extends ContextAwareActor {

    protected final TenantId tenantId;
    private final RuleChainService ruleChainService;
    @Getter
    protected RuleChain rootChain;
    @Getter
    protected KbActorRef rootChainActor;

    protected boolean ruleChainsInitialized;

    public RuleChainManagerActor(ActorSystemContext systemContext, TenantId tenantId) {
        super(systemContext);
        this.tenantId = tenantId;
        this.ruleChainService = systemContext.getRuleChainService();
    }

    protected void initRuleChains() {
        log.debug("[{}] Initializing rule chains", tenantId);
        List<RuleChain> ruleChainsByTenantId = ruleChainService.getRuleChainsByTenantId(tenantId.getId());
        for (RuleChain ruleChain : ruleChainsByTenantId) {
            String ruleChainId = ruleChain.getId();
            log.debug("[{}|{}] Creating rule chain actor", ruleChain.getEntityType(), ruleChainId);
            KbActorRef actorRef = getOrCreateActor(new RuleChainId(ruleChainId), id -> ruleChain);
            visit(ruleChain, actorRef);
            log.debug("[{}|{}] Rule Chain actor created.", ruleChain.getEntityType(), ruleChainId);
        }
        ruleChainsInitialized = true;
    }


    protected void visit(RuleChain entity, KbActorRef actorRef) {
        if (entity != null && entity.getRoot()) {
            rootChain = entity;
            rootChainActor = actorRef;
        }
    }

    protected KbActorRef getOrCreateActor(RuleChainId ruleChainId) {
        return getOrCreateActor(ruleChainId, eId -> ruleChainService.getRuleChainById(TenantId.SYS_TENANT_ID.getId()));
    }

    protected KbActorRef getOrCreateActor(RuleChainId ruleChainId, Function<RuleChainId, RuleChain> provider) {
        return ctx.getOrCreateChildActor(new KbEntityActorId(ruleChainId),
                () -> DefaultActorService.RULE_DISPATCHER_NAME,
                () -> {
                    RuleChain ruleChain = provider.apply(ruleChainId);
                    if (ruleChain == null) {
                        log.error("[{}|{}] RuleChain not found.", ruleChainId, ruleChainId);
                        return null;
                    } else {
                        return new RuleChainActor.ActorCreator(systemContext, tenantId, ruleChain);
                    }
                },
                () -> true);
    }

}
